<?php
/**
 * Created by PhpStorm.
 * User: huynguyen
 * Date: 9/19/14
 * Time: 10:02 PM
 */

namespace Backend\Modules\Video\Controllers;

//use Phalcon\Paginator\Adapter\NativeArray as Paginator;
use \Phalcon\Paginator\Adapter\Model as Paginator;
use Phalcon\Mvc\Model\Resultset;
use Library\Pagination\Pager as Pager;
use Backend\Modules\Video\Forms\FormCreateVideoCategory;
use Backend\Modules\Video\Models\VideoCategory;

class VideoCategoryController extends \BackendController
{
    public $urlVideoCategory = 'video/category';

    public function initialize()
    {
        
    }

    public function indexAction()
    {

        $currentPage = abs($this->request->getQuery('page', 'int', 1));
        $videoCategory = VideoCategory::find();
            
        if ($currentPage == 0) {
            $currentPage = 1;
        }

        $pager = new Pager(
            new Paginator(array(
                'data'  => $videoCategory,
                'limit' => 5,
                'page'  => $currentPage,
            )),
            array(
                // We will use Bootstrap framework styles
                'layoutClass' => 'Library\Pagination\Pager\Layout\Bootstrap',
                // Range window will be 5 pages
                'rangeLength' => 5,
                // Just a string with URL mask
                'urlMask'     => '?page={%page_number}',
                // Or something like this
                // 'urlMask'     => sprintf(
                //     '%s?page={%%page_number}',
                //     $this->url->get(array(
                //         'for'        => 'index:posts',
                //         'controller' => 'index',
                //         'action'     => 'index'
                //     ))
                // ),
            )
        );
        $this->view->pager = $pager;       
    }

    public function editAction($id)
    {

        $videoCategory = VideoCategory::findFirstById($id);
        if(!$videoCategory)
        {
            $this->flash->error("Video was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        $form = new FormCreateVideoCategory($videoCategory , array('edit' => true));

        if($this->request->getPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $videoCategory->assign(array(
                    'name' => $this->request->getPost('name'),
                    'description'   => $this->request->getPost('description'),
                    'parent_id'     => $this->request->getPost('parent_id') ? $this->request->getPost('parent_id') : 0,
                    'id_path'       => 1,
                    'position'     => $this->request->getPost('position') ? $this->request->getPost('position') : 0,
                    'status'            => $this->request->getPost('status')
                ));

                if($videoCategory->save())
                {
                    if($videoCategory->getParentId() > 0)
                    {
                        $this->updateIdPathVideoCategory($videoCategory);
                    }
                    return $this->dispatcher->forward(array(
                        'action' => 'index'
                    ));
                }
                $this->flash->error($videoCategory->getMessages());
            }
        }
        $this->view->form = $form;
    }

    public function createAction()
    {
        $form = new FormCreateVideoCategory();
        $videoCategory = new VideoCategory();

        //$form->bind($this->request->getPost() , $videoCategory);

        if($this->request->isPost())
        {
            if($form->isValid($this->request->getPost()) != false)
            {
                $videoCategory->assign(array(
                    'name' => $this->request->getPost('name'),
                    'description'   => $this->request->getPost('description'),
                    'parent_id'     => $this->request->getPost('parent_id') ? $this->request->getPost('parent_id') : 0,
                    'id_path'       => 1,
                    'position'     => $this->request->getPost('position') ? $this->request->getPost('position') : 0,
                    'status'            => $this->request->getPost('status')
                ));

                if($videoCategory->save())
                {
                    if($videoCategory->getParentId() > 0)
                    {
                        $this->updateIdPathVideoCategory($videoCategory);
                    }
                    return $this->response->redirect($this->getBackendUrl() . '/' . $this->urlVideoCategory);
                }
                $this->flash->error($videoCategory->getMessages());
            }
        }
        $this->view->form = $form;
    }

    public function deleteAction($id)
    {
        $videoCategory = VideoCategory::findFirstById($id);
        if(!$videoCategory)
        {
            $this->flash->error("Video was not found");
            return $this->dispatcher->forward(array(
                'action' => 'index'
            ));
        }
        if(!$videoCategory->delete())
        {
            $this->flash->error($videoCategory->getMessages());
        }
        else
        {
            $this->flash->success("Video was deleted");
        }

        return $this->dispatcher->forward(array(
            'action' => 'index'
        ));
    }

    public function updateIdPathVideoCategory(VideoCategory $videoCategory)
    {
        $id_path = $videoCategory->getId();
        $parent_id = $videoCategory->getParentId();
        if($parent_id > 0){
            do{
                if($parent_id > 0)
                {
                    $id_path .= '/' . $parent_id;
                    $parent = VideoCategory::findFirstById($parent_id);
                    if($parent){
                        $parent_id = $parent->getParentId();
                        $loop = true;
                    }
                    else
                    {
                        $loop = false;
                    }
                }
                else
                {
                    $loop = false;
                }
            }while($loop == true);
        }
        $videoCategory->setIdPath($id_path);
        $videoCategory->save();
    }

}
